package com.cuit.zuke.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.zuke.common.util.Constant;
import com.cuit.zuke.system.dao.SysMenuDao;
import com.cuit.zuke.system.entity.SysMenuEntity;
import com.cuit.zuke.system.service.SysAdminService;
import com.cuit.zuke.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: zuke
 * @description: 菜单管理实现类
 * @author: KamTang
 * @create: 2022-03-31 10:15
 **/
@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuDao, SysMenuEntity> implements SysMenuService {

    @Autowired
    private SysAdminService adminService;

    /**
     * 获取所有菜单
     *
     * @return
     */
    @Override
    public List<SysMenuEntity> getAllMenu() {
        return this.baseMapper.selectList(null);
    }

    /**
     * 获取导航菜单
     * @param userId id
     * @return 树形输出所有
     */
    @Override
    public List<SysMenuEntity> getNavMenu(Long userId) {
        // 系统管理员，开放所有权限
        if (userId == Constant.SUPER_ADMIN) {
            return getMenuListByMenuId(null);
        }
        // 用户菜单列表
        List<Long> menuIdList = adminService.getMenuIdByUserId(userId);
        return getMenuListByMenuId(menuIdList);
    }

    /**
     * 根据菜单id获取菜单列表
     *
     * @param menuIds
     * @return
     */
    private List<SysMenuEntity> getMenuListByMenuId (List<Long> menuIds) {
        List<SysMenuEntity> menus = this.baseMapper.selectList(new QueryWrapper<SysMenuEntity>()
                .in(Objects.nonNull(menuIds), "menu_id", menuIds).in("type", 0, 1));

        // 获取所有父级菜单
        List<SysMenuEntity> parentMenu = menus.stream().filter(sysMenu -> sysMenu.getParentId() == 0).collect(Collectors.toList());
        // 封装成父子孙结构
        List<SysMenuEntity> collect = parentMenu.stream().map((menu) -> {
            menu.setList(getChildren(menu, menus));
            return menu;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 根据父菜单，查询子菜单
     *
     * @param parentId 父菜单ID
     */
    @Override
    public List<SysMenuEntity> queryListParentId(Long parentId) {
        return baseMapper.queryListParentId(parentId);
    }

    /**
     * 删除
     *
     * @param menuId 菜单id
     */
    @Override
    public void delete(Long menuId) {
        baseMapper.deleteById(menuId);
    }

    /**
     * 获取不包含按钮的菜单列表
     *
     * @return List<SysMenuEntity>
     */
    @Override
    public List<SysMenuEntity> queryNotButtonList() {
        return baseMapper.queryNotButtonList();
    }

    /**
     * 递归获取所有子节点
     *
     * @param sysMenuEntity
     * @param sysMenuEntityList
     * @return
     */
    private List<SysMenuEntity> getChildren(SysMenuEntity sysMenuEntity, List<SysMenuEntity> sysMenuEntityList) {
        List<SysMenuEntity> children = sysMenuEntityList.stream().filter(sysMenu -> {
            return sysMenu.getParentId().equals(sysMenuEntity.getMenuId());
        }).map(sysMenu -> {
            sysMenu.setList(getChildren(sysMenu, sysMenuEntityList));
            return sysMenu;
        }).collect(Collectors.toList());
        return children;
    }

    // TOTO 修改菜单

}
